package face.pay.system;

import face.pay.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * SESSION 管理类
 */
@Slf4j
public class SessionUtils {

    private static final String CURRENT_USER_SRSSION = "CURENT_USER";

    /**
     * 将用户存入session
     *
     * @param request
     * @param userBase
     */
    public static void addUserToSession(HttpServletRequest request, UserInfo userInfo) {
        log.info("==============save user to session==============================");
        HttpSession session = request.getSession();
        session.setAttribute(CURRENT_USER_SRSSION, userInfo);
        log.debug(userInfo.toString());
        log.debug("==============save user to session end ==============================");

    }

    public static UserInfo getUserFromSession(HttpServletRequest request) {

        log.debug("==============get user from session==============================");
        HttpSession session = request.getSession();
        UserInfo user = (UserInfo) session.getAttribute(CURRENT_USER_SRSSION);
        if (user != null) {
            log.debug(user.toString());
        } else {
            log.debug("session user is null");
        }
        log.debug("==============get user from session==============================");

        return user;
    }

    public static void removeUserFromSession(HttpServletRequest request) {

        log.debug("==============remove user from session==============================");
        HttpSession session = request.getSession();
        UserInfo user = (UserInfo) session.getAttribute(CURRENT_USER_SRSSION);
        if (user != null) {
            session.removeAttribute(CURRENT_USER_SRSSION);
            log.debug(user.toString());
        } else {
            log.debug("session user is null");
        }
        log.debug("==============remove user from session==============================");


    }



}
